package cn.itcast.web.controller.company;

import cn.itcast.common.entity.PageResult;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController{
    @Autowired
    private CompanyService companyService;

   //展示企业
/*     @RequestMapping("/list")
    public String list(Model model){
        List<Company> list = companyService.findAll();
        model.addAttribute("list" ,list);
        return "company/company-list"; //抽取前缀和后缀
    }*/
   @RequestMapping("/list")
   public String list(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "5") int size){
     /*   //传统分页
       //1.调用service查询分页
       PageResult pr  = companyService.findPage(page,size);
       //2.将数据存入request域中
       request.setAttribute("page",pr);
       //3.返回跳转的页面*/

        //使用pagehelper 分页
        PageInfo info = companyService.findAllByHelper(page,size);
        model.addAttribute("page",info);

       return "company/company-list";
   }


    //跳转到添加页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        //int i = 1/0;
        return "company/company-add";
    }

/*    //编辑企业 保存   （跟新整合）
    @RequestMapping("/edit")
    public String edit(Company company){
             companyService.save(company);

        return "redirect:/company/list.do";
    }*/

    //跳转更新页面（根据id显示选中信息）
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Model model){
        //根据id查询企业信息
        Company company = companyService.findById(id);
        //存入到request域中
        model.addAttribute("company",company);
        //跳转页面
        return "company/company-update";
    }

    //保存   更新页面
    @RequestMapping("/edit")
    public String update(Company company){
        //判断属性id是否为空
        if(StringUtils.isEmpty(company.getId())){
            companyService.save(company);
        }else {
            //更新
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(String  id){
        companyService.delete(id);
        return "redirect:/company/list.do";
    }

}
